package export

type Option struct {
	Path         string
	IsCreateDB   bool
	IsUseDB      bool
	IsInsertLock bool
	AllData      bool
	IsTrig       bool
	IsFunc       bool
	IsView       bool
	IsProc       bool
	IsEvent      bool
}
type Genera struct {
	op     *Option
	DataCH chan string
	DoneCH chan bool
}

func (gen *Genera) GenFront(db *DataBases) {
	gen.sQLHead(db)
	if gen.op.IsCreateDB {
		gen.sqlCreateDB(db)
		gen.sqlUseDB(db)
	} else {
		if gen.op.IsUseDB {
			gen.sqlUseDB(db)
		}
	}
}
func (gen *Genera) sQLHead(db *DataBases) {
	contentHead := "/* \n mysql export tools for golang V0.1" +
		"\nMySQL - " + db.Info.Version +
		" : Database - " + db.Name +
		"\n*********************************************************************" +
		"\n*/\n\n"
	setModeHead := "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;" +
		"\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;" +
		"\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;" +
		"\n/*!40101 SET NAMES utf8 */;" +
		"\n/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;" +
		"\n/*!40103 SET TIME_ZONE='+00:00' */;" +
		"\n/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;" +
		"\n/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;" +
		"\n/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;" +
		"\n/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n"

	gen.DataCH <- contentHead + setModeHead
}

func (gen *Genera) sqlCreateDB(db *DataBases) {
	sql := "--\n-- Current Database: `" + db.Name + "`\n--\n\n" +
		db.DDL + ";\n\n"
	gen.DataCH <- sql

}
func (gen *Genera) sqlUseDB(db *DataBases) {
	sql := "USE `" + db.Name + "`;\n"
	gen.DataCH <- sql
}

func (gen *Genera) SQLTable(table *Table) {
	lock := ""
	if gen.op.IsInsertLock {
		lock = "LOCK TABLES `" + table.Name +
			"` WRITE;\n\n"
	}

	sql := "/*Table structure for table `" + table.Name +
		"` */\n\nDROP TABLE IF EXISTS `" + table.Name + "`;\n\n" +
		table.DDL +
		";\n\n--\n-- Dumping data for table `" + table.Name +
		"`\n--\n\n" + lock +
		"/*!40000 ALTER TABLE `" + table.Name +
		"` DISABLE KEYS */;\n"
	gen.DataCH <- sql
}

func (gen *Genera) SQLInsert(tableName, partData string) {
	sql := "insert  into `" + tableName +
		"` VALUES " + partData
	gen.DataCH <- sql
}

func (gen *Genera) SQLTableEnd(table *Table) {
	unlock := "\n"
	if gen.op.IsInsertLock {
		unlock = "UNLOCK TABLES;\n\n"
	}
	sql := "/*!40000 ALTER TABLE `" + table.Name +
		"` ENABLE KEYS */;\n" + unlock
	gen.DataCH <- sql
}

//
//  SQLTrig
//  @Description: 没使用
//  @receiver gen
//  @param name
//  @param data
//
func (gen *Genera) SQLTrig(name, data string) {
	sql := "DELIMITER $$\n*!50003 DROP TRIGGER*//*!50032 IF EXISTS */ /*!50003 `" + name +
		"` */$$\n" + data +
		"$$\n\nDELIMITER ;"
	gen.DataCH <- sql
}

func (gen *Genera) SQLFunc(name, data string) {
	sql := "/* Function  structure for function  `" + name +
		"` */\n\n/*!50003 DROP FUNCTION IF EXISTS `" + name +
		"` */;\nDELIMITER $$\n/*!50003 " + data +
		" */$$\nDELIMITER ;\n\n"
	gen.DataCH <- sql
}

func (gen *Genera) SQLProc(name, data string) {
	sql := "/* Procedure structure for procedure `" + name +
		"` */\n\n/*!50003 DROP PROCEDURE IF EXISTS  `" + name +
		"` */;\n\nDELIMITER $$\n\n/*!50003 " + data +
		" */$$\nDELIMITER ;\n\n"
	gen.DataCH <- sql
}

func (gen *Genera) SQLEvent(name, data string) {
	sql := "/*!50106 set global event_scheduler = 1*/;\n\n/* Event structure for event `" + name +
		"` */\n\n/*!50106 DROP EVENT IF EXISTS `" + name +
		"`*/;\n\nDELIMITER $$\n\n/*!50106 " + data +
		" */$$\nDELIMITER ;\n\n"
	gen.DataCH <- sql
}

func (gen *Genera) SQLView(name, data string) {
	sql := "/*View structure for view " + name +
		" */\n\n/*!50001 DROP TABLE IF EXISTS `" + name +
		"` */;\n/*!50001 DROP VIEW IF EXISTS `" + name +
		"` */;\n\n/*!50001 " + data +
		" */;\n\n"
	gen.DataCH <- sql
}

func (gen *Genera) sqlDataTemple(data string) {
	gen.DataCH <- data
}

func (gen *Genera) GenEnd() {
	sql := "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"
	gen.DataCH <- sql
}
